<!DOCTYPE html>
<html ng-app="pay" ng-controller="dispatcher">

<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no">
    <title></title>
    <script src="../js/angular-1.5.7.js"></script>
    <!-- wechat plug -->
    <script type="text/javascript" src="../js/plug/wechat/pingpp_pay.js"></script>
</head>
<script>
var pay = angular.module('pay', []);
pay.controller('dispatcher', function($scope, $http) {
    console.log('ok')
    var _ACCESSTOKEN_ = sessionStorage.getItem("accessToken");
    var _SERVER_ = "http://mobile-api.imlenong.com:8088/lenong";

    var myFarmId = sessionStorage.getItem("manege_myFarmId");
    var months = sessionStorage.getItem("manege_months");

    var xhr = new Jsonp($http);

    var get_indent_url = _SERVER_ + "/cropManageOrderAdd?access_token=" + _ACCESSTOKEN_ + "&myFarmId=" + myFarmId + "&months=" + months + "&callback=JSON_CALLBACK";
    log(get_indent_url)

    xhr.get(get_indent_url, function(data) {
        log(data);
        if (data.result == 1) {
            var indent_id = data.farmManageOrder.id;
            var pay_url = _SERVER_ + "/chargeCreate?access_token=" + _ACCESSTOKEN_ + "&orderId=" + indent_id + "&callback=JSON_CALLBACK";
            log(pay_url);
            pay(pay_url);
        } else {
            alert('系统繁忙，稍后尝试。');
            location.href = "http://wx.imlenong.com/myFarm.html";
        }
    });

    function pay(pay_url) {
        xhr.get(pay_url, function(data) {
            log(data);
            // alert(location.href)
            var charge = data.pingxxCharge;
            pingpp.createPayment(charge, function(result, err) {
                console.log(result);
                console.log(err.msg);
                console.log(err.extra);
                if (result == "success") {
                    log('pay ok');
                    location.href = "http://wx.imlenong.com/myFarm.html";
                    // 只有微信公众账号 wx_pub 支付成功的结果会在这里返回，其他的支付结果都会跳转到 extra 中对应的 URL。
                } else if (result == "fail") {
                    log('pay fail');
                    location.href = "http://wx.imlenong.com/myFarm.html";
                    // charge 不正确或者微信公众账号支付失败时会在此处返回
                } else if (result == "cancel") {
                    log('pay cancel');
                    location.href = "http://wx.imlenong.com/myFarm.html";
                    // 微信公众账号支付取消支付
                }
            });
        })
    }

    //jsonp
    function Jsonp($http) {
        this.get = function(url, callback) {
            $http.jsonp(url).success(function(data) {
                callback(data);
            }).error(function(data, header, config, status) {
                console.log(data);
                console.log(header);
                console.log(status);
                log('请检查网络连接')
            });
        }
    }

    var $$ = function(id) {
        return document.getElementById(id);
    };

    function log(content, title) {
        var args = Array.prototype.slice.call(arguments);
        if (!title) {
            title = '';
        } else {
            args.unshift(title + ' -- ');
            args.pop();
        }
        args.unshift('[app]:');
        console.log.apply(console, args);
    }
});
</script>

<body>
</body>

</html>
